package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_CollectDataServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataModel;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice_statusInfo.CollectDevice_StatusInfoModel;
import com.hzhr.iot.device.sdk.model.collectDevice_statusInfo.CollectDevice_StatusInfoPutModel;
import com.hzhr.iot.device.sdk.model.collectDevice_valveStatus.CollectDevice_ValveStatusPutModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDevicePutModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDevice_StatusInfoService;
import com.hzhr.iot.device.sdk.service.Device_CollectDevice_ValveStatusService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 集中器 单表实时抄表
 * @author 48310
 *
 */
@Component
public class MessageProcessor64 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	Device_CollectDevice_StatusInfoService device_collectDevice_statusInfoService;
	@Autowired
	Device_CollectDataService device_collectDataService;
	@Autowired
	Device_CollectDataServiceHelper device_collectDataServiceHelper;
	@Autowired
	Global global;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String ccrCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CONCENTRATOR_NO));
		String meterCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_METER_NO));
		String currentDate = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CURRENT_DATE));
		String currentTime = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CURRENT_TIME));
		String currentValueIntegerPart = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CURRENT_VALUE_INTEGER_PART));
		String currentValueDecimalPart = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_CURRENT_VALUE_DECIMAL_PART));
		Integer valveStatus = HzhrTypeUtil.object2integer(parsedMessageObj.get(MessageFieldKeyEnum.RESP_VALVE_STATUS));
		Integer collectStatus = HzhrTypeUtil.object2integer(parsedMessageObj.get(MessageFieldKeyEnum.RESP_COLLECT_STATUS));

		if (!StringUtils.isEmpty(ccrCode) && !StringUtils.isEmpty(meterCode)
				&& !StringUtils.isEmpty(currentDate) && !StringUtils.isEmpty(currentTime) &&
				!StringUtils.isEmpty(currentValueIntegerPart) && !StringUtils.isEmpty(currentValueDecimalPart)
				&& collectStatus != null && collectStatus == 1 ) {
			IotDeviceModel ccr = device_iotDeviceServiceHelper.getByCode(ccrCode);
			if (ccr != null) {
				Integer ccrId = ccr.getId();
				Integer meterId = device_collectDeviceServiceHelper.getIdByIotDeviceIdAndCode(ccrId, meterCode);

				if (meterId != null) {
					Date currentTm = HzhrDateUtil.strToDate(currentDate + currentTime, "yyyyMMddHHmmss");
					int count = device_collectDataServiceHelper.getCountByIotDeviceIdAndCollectDeviceIdAndCurrentTm(ccrId, meterId, currentTm);
					if (count == 0) {//如果采集数据不存在
						//记录采集数据
						CollectDataPostModel collectDataPostModel = new CollectDataPostModel();
						collectDataPostModel.setCollectDeviceId(meterId);
						collectDataPostModel.setIotDeviceId(ccrId);

						collectDataPostModel.setCurrentTm(currentTm.getTime());
						Double currentValue = new BigDecimal(currentValueIntegerPart + "." + currentValueDecimalPart).doubleValue();
						collectDataPostModel.setCurrentValue(currentValue);
						if (valveStatus == 0) {
							collectDataPostModel.setValveStatusCode("2");
						}
						if (valveStatus == 1) {
							collectDataPostModel.setValveStatusCode("1");
						}
						if (valveStatus == 2) {
							collectDataPostModel.setValveStatusCode("3");
						}
						Map<String, Object> query = new HashMap<>();
						device_collectDataService.post(global.initGlobalHeader(), query, collectDataPostModel);

						//更新设备状态信息
						CollectDevice_StatusInfoPutModel collectDevice_statusInfoPutModel = new CollectDevice_StatusInfoPutModel();
						collectDevice_statusInfoPutModel.setLastCollectTime(currentTm.getTime());
						collectDevice_statusInfoPutModel.setCurrentReading(currentValue);
						if (valveStatus == 0) {
							collectDevice_statusInfoPutModel.setCurrentValveStatusCode("2");
						}
						if (valveStatus == 1) {
							collectDevice_statusInfoPutModel.setCurrentValveStatusCode("1");
						}
						if (valveStatus == 2) {
							collectDevice_statusInfoPutModel.setCurrentValveStatusCode("3");
						}
						query = new HashMap<>();
						device_collectDevice_statusInfoService.putByCollectDeviceId(global.initGlobalHeader(), meterId, query, collectDevice_statusInfoPutModel);
					}
				}
			}
		}

		return null;
	}
}
